<template>
  <div class="login-page">
    <TitleHeader :title="title"></TitleHeader>
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="username" placeholder="请输入账号" />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <router-link to="/register" class="link">还没有账号？快去注册~</router-link>
  </div>
</template>

<script>
import request from "@/utils/request";
// import { Toast } from "vant";
export default {
  name: "login-page",
  data() {
    return {
      username: "chijiale",
      password: "123456",
      title: "账号登录",
    };
  },
  created() {},
  methods: {
    async onSubmit(values) {
      const res = await request({
        url: "/user/login",
        method: "post",
        data: { username: values.username, password: values.password },
      });
      if (res.status === 400) {
        this.$toast.fail(res.description);
      } else {
        // console.log(res);
        this.$toast.success(res.description);
        localStorage.setItem("token", res.body.token);

        setTimeout(() => {
          this.$router.back();
          clearTimeout();
        }, 1000);
      }
    },
    onClickLeft() {
      this.$router.go(-1);
      // Toast("返回");
    },
  },
};
</script>

<style lang="less" scoped>
.link {
  margin-top: 30px;
  color: #666;
  font-size: 13px;

  text-align: center;
  display: block;
}
.van-nav-bar {
  background-color: #21b97a;
}
::v-deep .van-nav-bar__title {
  color: #fff;
  font-size: 18px;
}
::v-deep .van-nav-bar .van-icon {
  color: #fff;
}
.van-form {
  margin-top: 21px;
}
::v-deep .van-field__control {
  font-size: 17px;
}
.van-button {
  font-size: 18px;
  margin-top: 30px;
  height: 48px;
}
.van-cell {
  height: 60px;
  line-height: 50px;
}
</style>
